﻿using System.Collections.Generic;
using Hopscotch.Common;

namespace Hopscotch.Presentation.Common.Events
{
    public class InteropBroadcastPayload
    {
        public int WorkSpaceId { get; private set; }
        private Dictionary<EntityType, int> intKeys;

        public InteropBroadcastPayload(int workSpaceId)
        {
            WorkSpaceId = workSpaceId;
        }

        public InteropBroadcastPayload(IShellContext workspaceContext)
        {
            WorkSpaceId = workspaceContext.WorkspaceId;
        }

        public void Add(EntityType entityType, int? id)
        {
            if (id.HasValue)
            {
                Add(entityType, id.Value);
            }
        }

        public void Add(EntityType entityType, int id)
        {
            if (intKeys == null)
                intKeys = new Dictionary<EntityType, int>();

            intKeys[entityType] = id;
        }

        public int GetIntId(EntityType entityType)
        {
            int id = 0;
            if (intKeys != null)
                intKeys.TryGetValue(entityType, out id);

            return id;
        }

        public static InteropBroadcastPayload Create(IShellContext workspaceContext, EntityType entityType, int id)
        {
            var payLoad = new InteropBroadcastPayload(workspaceContext.WorkspaceId);
            payLoad.Add(entityType, id);

            return payLoad;
        }
    }
}
